package cn.tedu.captcha_demo.controller;

import cn.tedu.captcha_demo.model.AuthenticationRequest;
import lombok.extern.slf4j.XSlf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * @Author: Miyako
 * @Date: 2024-08-29-16:49
 * @Description:
 */
@Controller
@RequestMapping("/login")
public class LoginController {



    @PostMapping
    public String login(AuthenticationRequest authenticationRequest, HttpSession session) {
        System.out.println("authenticationRequest = " + authenticationRequest);
        String username = authenticationRequest.getUsername();
        String password = authenticationRequest.getPassword();
        if (session == null) {
            throw new RuntimeException("验证码尚未生成");
        }
        Object code = session.getAttribute("code");
        if (Objects.isNull(code)) {
            throw new RuntimeException("验证码尚未生成");
        }

        String captcha = authenticationRequest.getCaptcha();

        if (!((String)code).equalsIgnoreCase(captcha)){
            return "failure";
        }

        if (("admin").equals(username)&&("password").equals(password)){
            return "success";
        }
        return "failure";

    }
}
